package com.app.utils.impl.labour;

import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;

import androidx.annotation.NonNull;

import com.app.utils.impl.IConsole;
import com.app.utils.impl.Strategy;
import com.zndroid.utils.XUtils;
import com.zndroid.utils.impl.NetUtil;

import java.math.BigInteger;

/**
 * Created by lzy on 2022/2/15.
 */
public class NetUtilStrategy extends Strategy {

    public NetUtilStrategy(IConsole iConsole) {
        super(iConsole);
    }

    @Override
    public void doing() {
        NetUtil util = XUtils.create().netUtil();

        iConsole.console("isNetAvailable", util.isNetAvailable(XUtils.getContext()) + "");
        iConsole.console("isWifiConnected", util.isWifiConnected(XUtils.getContext()) + "");
        iConsole.console("getNetType", util.getNetType(XUtils.getContext()) + "");

        if (android.os.Build.VERSION.SDK_INT > 9) {/*需要在子线程中请求，可以设置请求网络数据量很小*/
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            iConsole.console("getIpByHost(‘www.baidu.com’)", util.getIpByHost("www.baidu.com"));
        }

        iConsole.console("hideIpPart(‘168.22.33.45’)", util.hideIpPart("168.22.33.45"));

        iConsole.console("ipv4ToLong(‘168.25.4.107’)", util.ipv4IntValue("168.25.4.107") + "");
        iConsole.console("longToIPV4(‘2820211819’)", util.intToIPV4(1795430824));
        iConsole.console("ipv6ToBitInteger(‘2000::78f1:2bb5:e2ee:2d4f’)", util.ipv6ToBitInteger("2000::78f1:2bb5:e2ee:2d4f").toString());
        iConsole.console("bigIntegerToIPv6(‘42535295865117307941636620743105981775’)", util.bigIntegerToIPv6(new BigInteger("42535295865117307941636620743105981775")).toString());
        iConsole.console("isIPV4(‘123’)", util.isIPV4("123") + "");
        iConsole.console("isIPV4(‘172.64.10.23’)", util.isIPV4("172.64.10.23") + "");
        iConsole.console("isIPV6(‘123’)", util.isIPV6("123") + "");
        iConsole.console("isIPV6(‘2000::78f1:2bb5:e2ee:2d4f’)", util.isIPV6("2000::78f1:2bb5:e2ee:2d4f") + "");

        Handler handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if (msg.what == 100) {
                    iConsole.console("ping(‘168.22.33.45’)",  msg.obj + "");
                }
            }
        };

        new Thread(() -> {
            boolean b = util.ping("168.22.33.45", 2000);
            handler.sendMessageDelayed(handler.obtainMessage(100, b), 1000);
        }).start();

    }
}
